package com.jd.cs.config;

import com.jd.cs.error.ErrorCode;
import com.jd.cs.exception.APIException;
import com.jd.cs.vo.ResultVO;
import io.jsonwebtoken.SignatureException;
import org.springframework.http.HttpStatus;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingRequestHeaderException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author: lizhiwei
 * @date: 2020-06-22 15:32
 **/
@RestControllerAdvice
public class ExceptionControllerAdvice {

    /**
     * 未捕获异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public ResultVO<String> ExceptionHandler(Exception e) {
        return new ResultVO<>(ErrorCode.FAIL, e.getMessage());
    }

    /**
     * 未捕获异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(SignatureException.class)
    public ResultVO<String> SignatureExceptionHandler(Exception e) {
        return new ResultVO<>(ErrorCode.HEADER_PARAMETER_ERROR, e.getMessage());
    }

    /**
     * 自定义异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(APIException.class)
    public ResultVO<String> APIExceptionHandler(APIException e) {
        ErrorCode errorCode = ErrorCode.getByCode(e.getCode());
        if (errorCode == null) {
            errorCode = ErrorCode.FAIL;
        }
        return new ResultVO<>(errorCode, null);
    }

    /**
     * 参数校验异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResultVO<String> MethodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
        ObjectError objectError = e.getBindingResult().getAllErrors().get(0);
        // 注意哦，这里传递的响应码枚举
        return new ResultVO<>(ErrorCode.PARAMETER_ERROR, objectError.getDefaultMessage());
    }

    /**
     * 参数校验异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(MissingRequestHeaderException.class)
    public ResultVO<String> MissingRequestHeaderExceptionHandler(MissingRequestHeaderException e) {
        return new ResultVO<>(ErrorCode.HEADER_PARAMETER_ERROR, e.getMessage());
    }

    /**
     * asset校验异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(IllegalArgumentException.class)
    public ResultVO<String> IllegalArgumentExceptionHandler(MethodArgumentNotValidException e) {
        ObjectError objectError = e.getBindingResult().getAllErrors().get(0);
        // 注意哦，这里传递的响应码枚举
        return new ResultVO<>(ErrorCode.PARAMETER_ERROR, objectError.getDefaultMessage());
    }

    /**
     * spring参数绑定异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(MissingServletRequestParameterException.class)
    public ResultVO<String> MissingServletRequestParameterExceptionHandler(MissingServletRequestParameterException e) {
        return new ResultVO<>(ErrorCode.PARAMETER_ERROR, e.getMessage());
    }
}